package com.atguigu.yygh.hosp.controller.api;


import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.hosp.utils.HttpRequestHelper;
import com.atguigu.yygh.hosp.utils.MD5;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.ScheduleOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;



    @ApiOperation(value = "删除排班")
    @PostMapping("schedule/remove")
    public R removeSchedule(HttpServletRequest request) {
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        String  hoscode = (String) map.get("hoscode");
        String  hosScheduleId = (String) map.get("hosScheduleId");

        scheduleService.remove(hoscode,hosScheduleId);

        return R.ok();
    }


    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public R schedule(HttpServletRequest request) {
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) map.get("hoscode");

        int page = StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String)map.get("page"));
        int limit = StringUtils.isEmpty(map.get("limit")) ? 10 : Integer.parseInt((String)map.get("limit"));

        Page<Schedule> pages = scheduleService.selectPageSche(page,limit);
        return R.ok(pages);
    }




    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public R saveSchedule(HttpServletRequest request) {
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());

        String hoscode = (String) map.get("hoscode");
        scheduleService.saveSchedule(map);
        return R.ok();
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public R removeDepartment(HttpServletRequest request) {
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) map.get("hoscode");
        String depcode = (String) map.get("depcode");
        departmentService.remove(hoscode,depcode);
        return R.ok();
    }


    @ApiOperation(value = "获取科室分页列表")
    @PostMapping("department/list")
    public R department(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        int page = StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String)map.get("page"));
        int limit = StringUtils.isEmpty(map.get("limit")) ? 10 : Integer.parseInt((String)map.get("limit"));

        Page<Department> pages = departmentService.selectPageDept(page,limit);
        return R.ok(pages);
    }



    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public R saveDepartment(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        departmentService.saveDepartment(map);

        return R.ok();
    }

    @ApiOperation(value = "获取医院信息")
    @PostMapping("hospital/show")
    public R hospital(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        String hoscode = (String)map.get("hoscode");
        Hospital hospital = hospitalService.findHospHoscode(hoscode);
        return R.ok(hospital);
    }


    @ApiOperation("上传医院")
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        String signHosp = (String) map.get("sign");
        String hoscode = (String) map.get("hoscode");
        String signKeyYygh = hospitalSetService.getSignKeyByHoscode(hoscode);
        String encryptMD5 = MD5.encrypt(signKeyYygh);

        if (!signHosp.equals(encryptMD5)){
            throw new YyghException(20001,"签名失败");
        }

        String logoData = (String) map.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        map.put("logoData",logoData);

        hospitalService.saveHosp(map);
        return Result.ok().code(200);

    }
    @ApiOperation(value = "根据排班id获取预约下单数据")
    @GetMapping("inner/getScheduleOrderVo/{scheduleId}")
    public ScheduleOrderVo getScheduleOrderVo(
            @ApiParam(name = "scheduleId", value = "排班id", required = true)
            @PathVariable("scheduleId") String scheduleId) {
        return scheduleService.getScheduleOrderVo(scheduleId);
    }

}
